package org.dashboard.client.widget.refreshingimage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Node;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.Widget;
import org.dashboard.client.widget.ImageWidget;

/**
 * @author Simon Vogensen, Lakeside
 * @version $Revision$
 */
public class RefreshingImageWidget extends ImageWidget {

    interface Binder extends UiBinder<Widget, RefreshingImageWidget> { }
    private static final Binder binder = GWT.create(Binder.class);

    public RefreshingImageWidget(final String url, String width, String height, int refreshInterval) {
        super();

        setWidget(binder.createAndBindUi(this));

        Node node = getElement().getChild(0);
        DOM.setStyleAttribute((Element) node, "width", width);
        DOM.setStyleAttribute((Element) node, "height", height);

        if(refreshInterval > 0) {
            final Timer timer = new Timer() {
                @Override
                public void run() {
                    replaceImageNoCache(url);
                }
            };
            timer.scheduleRepeating(refreshInterval);
            replaceImageNoCache(url);
        }
    }
}
